package com.woniu.darryringmall.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.darryringmall.front.entity.Activity;
import com.woniu.darryringmall.front.entity.ActivitySpec;
import com.woniu.darryringmall.front.entity.User;
import com.woniu.darryringmall.front.service.ActivityService;
import com.woniu.darryringmall.front.service.ActivitySpecService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("front/active")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @Autowired
    private ActivitySpecService activitySpecService;


    /**
     * 获取现在所有没有结束的活动详情
     * @return
     */
    @PostMapping("list")
    public ResponseData list(Integer current){
        if (current==null){
            current=1;
        }
        Page<Activity> page=new Page<>(current,8);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("isover","n");
        page=activityService.page(page,queryWrapper);
        return new ResponseData(page);
    }

    /**
     * 用户参加活动
     */
    @PostMapping("userActivity")
    public ResponseData userActivity(int id, HttpSession session){
        User user=(User)session.getAttribute("user");
        ActivitySpec activitySpec=new ActivitySpec();
        activitySpec.setActivityId(id);
        activitySpec.setUserId(user.getId());
        activitySpecService.add(activitySpec);
        return ResponseUtils.SUCCESS;
    }

    /**
     * 用户取消活动
     */
    @PostMapping("cancel")
    public ResponseData cancel(int id, HttpSession session){
        User user=(User)session.getAttribute("user");
        activitySpecService.delete(id,user.getId());
        return ResponseUtils.SUCCESS;
    }

    /**
     * 获取参加活动人数
     */
    @PostMapping("peopleNums")
    public ResponseData peopleNums(int id){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("activityId",id);
        int nums= activitySpecService.count(queryWrapper);
        return new ResponseData(nums);
    }

}
